package com.hamster.crmpre.web;

import com.hamster.crmpre.mapper.CustomerMapper;
import com.hamster.crmpre.mapper.ProjectMapper;
import com.hamster.crmpre.pojo.*;
import com.hamster.crmpre.pojo.dto.ChangeCourseDTO;
import com.hamster.crmpre.pojo.dto.ChangeCourseDTOParent;
import com.hamster.crmpre.pojo.dto.CourseDetailsDTO;
import com.hamster.crmpre.pojo.dto.ProjectCourseClassTime;
import com.hamster.crmpre.service.CourseDetailsService;
import com.hamster.crmpre.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 课程明细的Controller
 * @author Tarry
 * @create 2019/12/28 11:02
 */
@RestController
@RequestMapping("courseDetails")
public class CourseDetailsController {
    @Autowired
    private CourseDetailsService courseDetailsService;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private ProjectMapper projectMapper;
    /**
     * 根据日期和客户id 获取当前课程时间列表
     //* @param currentDate 当前日期
     * @param customerId  客户的id
     * @return
     */
    @GetMapping("getCourseDetails")
    public ResponseEntity<List<ProjectCourseClassTime>> getCourseDetails(
//            @RequestParam
//            @DateTimeFormat(pattern="yyyy-MM-dd")
//            Date currentDate,
            @RequestParam
            String customerId
    ){
//        List<CustomerCourseClass> list = courseDetailsService.getCourseDetails(currentDate,customerId);
        List<CustomerCourseClass> list = courseDetailsService.getChangeCourseDetails(customerId);
        System.out.println( JsonUtils.serialize(list));
        //上面代码，找出了课程及上课时间。下面代码 把课程放入规定的 项目内
        Customer customer = customerMapper.selectById(customerId);
        List<String> projectList = JsonUtils.parseList(customer.getProjectId(), String.class);
        List<ProjectCourseClassTime> projectCourseClassTimeList = new ArrayList<>();
        //TODO 修改完毕
        for (String pid : projectList) {
            ProjectCourseClassTime projectCourseClassTime = new ProjectCourseClassTime();
            Project project = projectMapper.selectById(pid);
            projectCourseClassTime.setProjectId(pid);
            projectCourseClassTime.setProjectTitle(project.getTitle());
            List<CustomerCourseClass> courseList = new ArrayList<>();
            for (CustomerCourseClass customerCourseClass : list) {
                if (pid.equals(customerCourseClass.getProjectId())){
                    courseList.add(customerCourseClass);
                }
            }
            projectCourseClassTime.setCourseList(courseList);
            projectCourseClassTimeList.add(projectCourseClassTime);
        }

        return ResponseEntity.ok(projectCourseClassTimeList);
    }

    /**
     * 保存 课程上课明细
     * @param  courseDetails  客户详情明细
     * @return
     */
//    @PostMapping("saveCourseDetails")
//    public ResponseEntity<Void> saveCourseDetails(@RequestBody List<CustomerCourseClass> courseDetails ){
//        courseDetailsService.saveCourseDetails(courseDetails);
//        return ResponseEntity.ok().build();
//    }
    @PostMapping("saveCourseDetails")
    public ResponseEntity<Void> saveCourseDetails(@RequestBody List<CustomerCourseClassTime> timeList ){
        courseDetailsService.saveCourseDetails2(timeList);
        return ResponseEntity.ok().build();
    }


    /**
     * 更改课程页面获取，用户全部课程
     * @param customerId
     * @return
     */
    @GetMapping("getChangeCourseDetails")
    public ResponseEntity<List<CustomerCourseClass>> getChangeCourseDetails(@RequestParam String customerId){
        List<CustomerCourseClass> list = courseDetailsService.getChangeCourseDetails(customerId);
        return ResponseEntity.ok(list);
    }



    /**
     * 更改课程
     * @return
     */
    @PostMapping("changeCourse")
    public ResponseEntity<Void> changeCourse(@RequestBody ChangeCourseDTOParent changeCourseDTOParent){
        courseDetailsService.changeCourse(changeCourseDTOParent);
        return ResponseEntity.ok().build();
    }


    /**
     * 更改课程页面获取，课程select下拉框
     * @return
     */
    @GetMapping("getCourseSelect")
    public ResponseEntity<List<ProjectCourse>> getCourseSelect(){
        List<ProjectCourse> list = courseDetailsService.getCourseSelect();
        return ResponseEntity.ok(list);
    }


}
